<?php
	
	
	
	$app->get('/tipoTarea/listar', function () use($app){
    	$app->render('../web/pages/tipoTarea/tipoTareaListar.php');
	});
	
	$app->get('/tipoTarea/listarJson', function (){
		$response = new stdClass();	
		$page = $_GET['page']; // get the requested page 
		$limit = $_GET['rows']; // get how many rows we want to have into the grid
    	$idEmpresa = $_SESSION['idEmpresa'];
    	$count = TipoTarea::getCantRows($idEmpresa);
    	if( $count >0 ) {
    		 $total_pages = ceil($count/$limit); 
		}else {
			 $total_pages = 1; 
		}
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		$tipoTareas = TipoTarea::listarInicioFin($idEmpresa, $start, $limit);
    	$i = 0;
		$response->page = $page; 
		$response->total = $total_pages; 
		$response->records = $count;
		foreach ($tipoTareas as $tiposTar) {
    		$response->gridModel[$i]['idTipoTarea']=$tiposTar->idTipoTarea;
			$response->gridModel[$i]['descripcion']=$tiposTar->descripcion;
			$response->gridModel[$i]['duracion']=$tiposTar->duracion;
			$i++;
		}
		echo json_encode($response);
	});
	
	$app->post('/tipoTarea/administrar', function (){
		$tipoTarea = null;
		$idTipoTarea = null;
		$idEmpresa = $_SESSION['idEmpresa'];
		if($_POST['id'] != "_empty" ){
			$idTipoTarea = $_POST['id'];	
		}
		//traigo el tipoTarea
		if($_POST['oper'] != "add"){
			$tipoTarea = TipoTarea::buscarTipoTarea($idTipoTarea);
		}
		if($_POST['oper'] == "del"){
			TipoIdentificacion::borrarTipoIdentificacion($idTipoIdentificacion);
		}else{
			//tipoIdentificacion nuevo
			if($tipoTarea == null && $_POST['oper'] == "add"){
				$tipoTarea = Model::factory('tipotarea')->create();	
			}
			$tipoTarea->idEmpresa = $idEmpresa;
			$tipoTarea->descripcion = $_POST['descripcion'];
			$tipoTarea->duracion = $_POST['duracion'];
			$tipoTarea->save();
		}
	});
		

?>